#!/usr/bin python3
# -*- coding: utf-8 -*-
""" 数据库定义和相关操作定义文件  """
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from sqlalchemy.ext.declarative import declarative_base

from gtm_server.common.constant import DatabaseConfig


# 数据库表对象的基类
Base = declarative_base()


class DBPool():
    """
    数据库操作定义基类
    """
    def __init__(self):
        self.database_url = DatabaseConfig.DATABASE_URL
        self.database_port = DatabaseConfig.DATABASE_PORT
        self.user = DatabaseConfig.USER
        self.password = DatabaseConfig.PASSWORD
        self.database_name = DatabaseConfig.DATABASE_NAME

    def get_engine(self):
        """
        获取数据库引擎
        """
        # enagine = create_engine(f"postgresql+psycopg2://{self.user}: \ "
        #                         f"{self.password}@{self.database_url}: \ "
        #                         f"{self.database_port}/{self.database_name}")

        engine = create_engine(
            "postgresql+psycopg2://postgres:12345@120.25.204.182:5432/postgres")

        return engine

    def get_session(self):
        """
        获取数据库连接
        """
        engine = self.get_engine()
        DBSession = sessionmaker(bind=engine)

        return DBSession()
